﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Win10WallpaperFetcher.Model;


namespace Win10WallpaperFetcher
{
    class Fetcher
    {
        private readonly FetchParam _fetchParam;

        public Fetcher(FetchParam fetchParam)
        {
            this._fetchParam = fetchParam;
        }

        /// <summary>
        /// 从文件夹中获取指定条件的图片
        /// </summary>
        /// <param name="dir">缓存图片的目录</param>
        /// <returns>所有的图片文件信息</returns>
        public List<FileInfo> GetWallpaperFiles(string dir)
        {
            List<string> files = Directory.GetFiles(dir).ToList();
            List<FileInfo> selectFiles = new List<FileInfo>();
            foreach (string file in files)
            {
                FileInfo info = new FileInfo(file);
                if (info.Length >= 1 * 1024 && string.IsNullOrWhiteSpace(info.Extension))
                {
                    Image image;
                    try
                    {
                        image = Image.FromFile(info.FullName);
                    }
                    catch (Exception)
                    {
                        continue;
                    }

                    if (image.Width > _fetchParam.MinWidth && image.Height > _fetchParam.MinHeight)
                    {
                        // 横屏
                        if (_fetchParam.IsOnlyFetchHorizontal)
                        {
                            if (image.Width >= image.Height)
                            {
                                selectFiles.Add(info);
                            }
                        }
                        // 竖屏
                        else if (_fetchParam.IsOnlyFetchVertical)
                        {
                            if (image.Height >= image.Width)
                            {
                                selectFiles.Add(info);
                            }
                        }
                        else
                        {
                            selectFiles.Add(info);
                        }
                    }
                }
            }
            return selectFiles;
        }

        /// <summary>
        /// 获取Win10保存壁纸文件的缓存目录
        /// </summary>
        /// <returns></returns>
        public static string GetWallpaperDirectory()
        {
            string appData = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Packages");
            List<string> dirs = Directory.GetDirectories(appData).ToList();
            string theContentDeliveryManagerDir =
                dirs.First(d => d.Contains("Microsoft.Windows.ContentDeliveryManager"));
            return Path.Combine(appData, theContentDeliveryManagerDir, "LocalState\\Assets");
        }

        /// <summary>
        /// 将图片拷贝到目标目录，同名将被覆盖
        /// </summary>
        /// <param name="files"></param>
        /// <param name="targetPath"></param>
        public static void CopyFiles(List<FileInfo> files, string targetPath)
        {

            if (!Directory.Exists(targetPath))
            {
                Directory.CreateDirectory(targetPath);
            }
            foreach (FileInfo fileInfo in files)
            {
                string extension = string.Empty;
                if (string.IsNullOrEmpty(fileInfo.Extension))
                {
                    extension = ".jpg";
                }
                fileInfo.CopyTo(Path.Combine(targetPath, fileInfo.Name) + extension, true);
            }
        }

    }
}
